package com.bckj.freshnow.order.trade.execution.create;

import com.bckj.fastboot.core.execution.Execution;
import com.bckj.fastboot.core.execution.ExecutionContext;
import com.bckj.freshnow.goods.model.GoodsSkuResponse;
import com.bckj.freshnow.goods.service.GoodsSkuService;
import com.bckj.freshnow.order.model.CreateTradeOrderRequest;
import com.bckj.freshnow.order.model.TradeItemRequest;
import com.bckj.freshnow.order.trade.fsm.TradeOrderContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class CreateTradeOrderGoodsInitExecution implements Execution {

    private final GoodsSkuService goodsSkuService;

    @Override
    public void execute(ExecutionContext context) {
        TradeOrderContext ctx = (TradeOrderContext) context;
        //商品信息初始化
        CreateTradeOrderRequest request = (CreateTradeOrderRequest) ctx.getRequest();
        List<String> skuCodeList = request.getItemList().stream().map(TradeItemRequest::getSkuCode).toList();
        Map<String, GoodsSkuResponse> skuInfo = goodsSkuService.findBySkuCodes(skuCodeList).stream().collect(Collectors.toMap(GoodsSkuResponse::getSkuCode, v -> v, (v1, v2) -> v1));
        ctx.setSkuInfo(skuInfo);
    }
}